package com.hisan.freeride.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.facebook.drawee.view.SimpleDraweeView;
import com.github.library.BaseQuickAdapter;
import com.github.library.BaseViewHolder;
import com.hisan.freeride.R;
import com.hisan.freeride.common.utils.ButtonUtils;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.utils.TokenUtils;
import com.hisan.freeride.home.model.WelfareModel;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.ufreedom.uikit.FloatingText;

import java.util.ArrayList;
import java.util.List;

/**
 * 公益广场列表适配器
 * Created by yu on 2017/1/17.
 */

public class WelfareAdapter extends BaseQuickAdapter<WelfareModel,BaseViewHolder> implements NineGridView.ImageLoader {
    List<ImageInfo>imageInfo;
    private Activity activity;
    private ButtonClickListener mButtonClickListener;
    private onItemClickListener mOnItemClickListener;


    public WelfareAdapter(List<WelfareModel> data, Activity activity) {
        super(R.layout.welfare_item,data);
        this.activity=activity;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final WelfareModel item) {
        if (!CollectionUtils.isNullOrEmpty(item.getAvatar())){
            SimpleDraweeView simpleDraweeView= holder.getView(R.id.logo);
            simpleDraweeView.setImageURI(item.getAvatar());
            simpleDraweeView.setOnClickListener(v -> {
                if (mButtonClickListener!=null){
                    mButtonClickListener.onClick(3,item);
                }
            });
        }

        holder.getView(R.id.welfare).setOnClickListener(v -> {
            if (mOnItemClickListener!=null){
                mOnItemClickListener.onItemClick(item);
            }
        });
        holder.setText(R.id.name, item.getRealname());
        holder.setText(R.id.data, item.getCreate_time());
        holder.setText(R.id.address, item.getInitially()+"-"+item.getDestination());
        ImageView imageView= holder.getView(R.id.sex);
        if (item.getSex().equals("男")){
            imageView.setImageResource(R.mipmap.male);
        }else{
            imageView.setImageResource(R.mipmap.female);
        }
        holder.setText(R.id.owner, item.getDriver_name());
        holder.getView(R.id.owner).setOnClickListener(v -> {
            if (mButtonClickListener!=null){
                mButtonClickListener.onClick(2,item);
            }
        });
        holder.setText(R.id.content, item.getContent());
        LinearLayout layout= holder.getView(R.id.wliner);
        if (!CollectionUtils.isNullOrEmpty(layout.getChildCount()))layout.removeAllViews();
        View view=LayoutInflater.from(mContext).inflate(R.layout.dianzan,null);
        if (view!=null){
            LinearLayout dianzan= (LinearLayout) view.findViewById(R.id.dian);
            LinearLayout pr= (LinearLayout) view.findViewById(R.id.pr);
            pr.setOnClickListener(v -> mOnItemClickListener.onItemClick(item));
            TextView praise= (TextView) view.findViewById(R.id.praise);
            praise.setText(item.getZans()+"");
            TextView comment= (TextView) view.findViewById(R.id.comment);
            comment.setText(item.getComments()+"");
            ImageView view1= (ImageView) view.findViewById(R.id.zan);
            dianzan.setOnClickListener(v -> {
                if (mButtonClickListener!=null){
                    if (item.getIs_zan()==1){
                        String token= TokenUtils.getToken();
                        if (!CollectionUtils.isNullOrEmpty(token)) {
                            mButtonClickListener.onClick(0, item);
                        }else {
                            mButtonClickListener.onClick(4, item);
                        }
                    }else {
                        if (!ButtonUtils.isFastDoubleClick()){
                            String token= TokenUtils.getToken();
                            if (!CollectionUtils.isNullOrEmpty(token)){
                                FloatingText floatingText = new FloatingText.FloatingTextBuilder(activity)
                                        .textColor(Color.RED) // 漂浮字体的颜色
                                        .textSize(24)   // 浮字体的大小
                                        .textContent("+1") // 浮字体的内容
                                        .offsetY(50) // FloatingText 相对其所贴附View的垂直位移偏移量
                                        .build();
                                floatingText.attach2Window(); //将FloatingText贴附在Window上
                                //启动漂浮效果
                                floatingText.startFloating( holder.getView(R.id.zan)); // 传入一个View，FloatingText 就会相对于这个View执行漂浮效果
                                if (mButtonClickListener!=null){
                                    mButtonClickListener.onClick(1,item);
                                }
                            }else {
                                if (mButtonClickListener!=null){
                                    mButtonClickListener.onClick(4,item);
                                }
                            }

                        }
                    }
            }
            });
            if (item.getIs_zan() ==1) {
                view1.setImageResource(R.mipmap.zan_fill);
            } else {
                view1.setImageResource(R.mipmap.zan);
            }
            layout.addView(view);
        }
        NineGridView nineGridView= holder.getView(R.id.nineGrid);
        if (!CollectionUtils.isNullOrEmpty(item.getAlbum())) {
            nineGridView.setVisibility(View.VISIBLE);
            imageInfo=new ArrayList<>();
            for (String  iamge : item.getAlbum()) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(iamge);
                info.setImageViewHeight(50);
                info.setImageViewWidth(50);
                info.setBigImageUrl(iamge);
                imageInfo.add(info);
            }
            nineGridView.setMaxSize(imageInfo.size());
            nineGridView.setSingleImageSize(200);
            nineGridView.setImageLoader(this);
            nineGridView.setAdapter(new NineGridViewClickAdapter(mContext, imageInfo));
        }else{
            nineGridView.setVisibility(View.GONE);
        }

    }

    @Override
    public void onDisplayImage(Context context, ImageView imageView, String url) {
        Glide.with(context).load(url)//
                .placeholder(R.drawable.ic_default_color)//
                .error(R.drawable.ic_default_color)//
                .diskCacheStrategy(DiskCacheStrategy.ALL)//
                .into(imageView);
    }

    @Override
    public Bitmap getCacheImage(String url) {
        return null;
    }

    public void setOnItemClickListener(onItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public void setButtonClickListener(ButtonClickListener listener) {
        mButtonClickListener = listener;
    }
    /**
     * 自定义的recyclerView点击监听
     */
    public interface onItemClickListener {
        void onItemClick(WelfareModel data);
    }

    /**
     * 按钮的点击监听
     */
    public interface ButtonClickListener {
        void onClick(int type, WelfareModel data);
    }


}
